# -*- encoding=utf8 -*-
__author__ = "Administrator"

from airtest.core.api import *

auto_setup(__file__)
def height_width():
    global width
    global height
    if G.DEVICE.display_info['orientation'] in [1,3]:        
        height = G.DEVICE.display_info['width']        
        width = G.DEVICE.display_info['height']
    else:
        height = G.DEVICE.display_info['height']
        width = G.DEVICE.display_info['width']
    global half_num
    half_num = (0.5*width,0.5*height)  
    return [height,width] 
def h_w_num(num_01,num_02):
    return (num_01*width,num_02*height)
def true_touch(v):
    if exists(v):
        touch(v)
def sign_in(id_pcr):
    stop_app("jp.co.cygames.princessconnectredive")
    sleep(2)
    start_app("jp.co.cygames.princessconnectredive")
    sleep(10)
    touch(wait(Template(r"tpl1598932608577.png", record_pos=(-0.305, -0.241), resolution=(960, 540))))
    touch(wait(Template(r"tpl1598932635861.png", record_pos=(0.114, 0.173), resolution=(960, 540))))
    touch(wait(Template(r"tpl1598932661647.png", record_pos=(0.001, -0.103), resolution=(960, 540))))
    touch(Template(r"tpl1598932686701.png", rgb=True, target_pos=8, record_pos=(-0.155, -0.097), resolution=(960, 540)))
    text(id_pcr)
    touch(wait(Template(r"tpl1598933960808.png", record_pos=(0.428, 0.21), resolution=(960, 540))))
    touch(Template(r"tpl1598932774477.png", rgb=True, target_pos=8, record_pos=(-0.153, -0.01), resolution=(960, 540)))

    text("123456")
    touch(wait(Template(r"tpl1598933960808.png", record_pos=(0.428, 0.21), resolution=(960, 540))))
    sleep(2)
    touch(Template(r"tpl1598932807818.png", record_pos=(0.111, 0.172), resolution=(960, 540)))
    touch(wait(Template(r"tpl1598934032694.png", record_pos=(0.115, 0.217), resolution=(960, 540))))
    sleep(2)
    touch(wait(Template(r"tpl1598934032694.png", record_pos=(0.115, 0.217), resolution=(960, 540))))
    touch(wait(Template(r"tpl1598934271647.png", threshold=0.8, rgb=True, record_pos=(-0.002, 0.105), resolution=(960, 540))))
    sleep(2)
    touch(wait(Template(r"tpl1598934271647.png", record_pos=(-0.002, 0.105), resolution=(960, 540))))
    touch(wait(Template(r"tpl1598934482893.png", record_pos=(-0.116, 0.11), resolution=(960, 540))))
    
    login_two()
def login_two():
    touch(v=half_num,times=50,duration=0.1)
    touch(Template(r"tpl1598946172730.png", record_pos=(-0.116, 0.105), resolution=(960, 540)))
    touch(Template(r"tpl1598946182366.png", record_pos=(-0.113, 0.103), resolution=(960, 540)))
    sleep(5)
    if exists(Template(r"tpl1598946200403.png", record_pos=(-0.116, 0.103), resolution=(960, 540))):
        touch(wait(Template(r"tpl1598934271647.png", record_pos=(-0.002, 0.105), resolution=(960, 540))))
    sleep(8)    
    touch(v=half_num,times=5,duration=0.1)
    touch(wait(Template(r"tpl1598946301051.png", record_pos=(0.446, -0.243), resolution=(960, 540))))
    touch(v=half_num,times=50,duration=0.1)

def import_id():
    fo = open("F://util//pcr//pcr.txt","r+")
    str_lines = fo.readlines()
    return str_lines;
def login_system():
    sleep(5)
    touch(wait(Template(r"tpl1598934861780.png", record_pos=(-0.004, 0.217), resolution=(960, 540))))
    sleep(5)
    touch(v=half_num,times=50,duration=0.1)
    touch(wait(Template(r"tpl1598934943880.png", record_pos=(0.367, 0.167), resolution=(960, 540))))
    touch(v=half_num,times=50,duration=0.1)
    sleep(3)
    touch(wait(Template(r"tpl1598934992373.png", record_pos=(0.067, -0.25), resolution=(960, 540))))
    sleep(3)
    touch(wait(Template(r"tpl1598935016709.png", record_pos=(0.372, 0.175), resolution=(960, 540))))
    touch(wait(Template(r"tpl1598935038858.png", record_pos=(-0.001, 0.217), resolution=(960, 540))))
    sleep(2)
def set_one_movie():
    touch(wait(Template(r"tpl1598946578292.png", record_pos=(0.407, 0.252), resolution=(960, 540))))
    sleep(2)
    true_touch(Template(r"tpl1598946519251.png", record_pos=(-0.007, 0.171), resolution=(960, 540)))

    touch(Template(r"tpl1598936373424.png", record_pos=(-0.34, -0.086), resolution=(960, 540)))
    touch(Template(r"tpl1598936384679.png", record_pos=(0.114, -0.182), resolution=(960, 540)))
    touch(Template(r"tpl1598936393719.png", record_pos=(0.272, -0.031), resolution=(960, 540)))

    touch(Template(r"tpl1598936407516.png", record_pos=(0.002, 0.22), resolution=(960, 540)))
    touch(Template(r"tpl1598936423373.png", record_pos=(-0.401, 0.247), resolution=(960, 540)))
def fight_pcr_in():    
    touch(Template(r"tpl1598935055474.png", record_pos=(0.002, 0.245), resolution=(960, 540)))
    touch(Template(r"tpl1598935075980.png", record_pos=(0.082, -0.069), resolution=(960, 540)))
def kekeluo_face():
    if exists(Template(r"tpl1598935904127.png", record_pos=(0.009, -0.045), resolution=(960, 540))):
        touch(v=half_num,times=100)
        sleep(10)
        if exists(Template(r"tpl1598936784215.png", record_pos=(-0.14, 0.259), resolution=(960, 540))):

            touch(Template(r"tpl1598936784215.png", record_pos=(-0.14, 0.259), resolution=(960, 540)))
            sleep(2)
            if exists(Template(r"tpl1598935987577.png", threshold=0.9, rgb=True, record_pos=(0.232, 0.079), resolution=(960, 540))):
                touch(Template(r"tpl1598935987577.png", threshold=0.9, rgb=True, record_pos=(0.232, 0.079), resolution=(960, 540)))
                touch(Template(r"tpl1598935995478.png", record_pos=(0.196, -0.141), resolution=(960, 540)))
                touch(Template(r"tpl1598936004936.png", record_pos=(0.216, -0.021), resolution=(960, 540)))
                touch(Template(r"tpl1598936012768.png", record_pos=(-0.005, 0.105), resolution=(960, 540)))
                skip_pcr()
                touch(v=half_num,times=20)
                sleep(5)
                return
            if exists(Template(r"tpl1598936888698.png", threshold=0.9, rgb=True, record_pos=(0.247, -0.127), resolution=(960, 540))):
                touch(Template(r"tpl1598936888698.png", threshold=0.9, rgb=True, record_pos=(0.247, -0.127), resolution=(960, 540)))
                touch(Template(r"tpl1598936960654.png", threshold=0.9, rgb=True, record_pos=(0.029, -0.177), resolution=(960, 540)))
                touch(Template(r"tpl1598936976131.png", threshold=0.9, rgb=True, record_pos=(0.027, 0.048), resolution=(960, 540)))
                sleep(2)
                touch(Template(r"tpl1598937009326.png", record_pos=(-0.001, 0.087), resolution=(960, 540)))
                skip_pcr()
                touch(v=half_num,times=20)
                skip_pcr()
                touch(v=half_num,times=20)
                touch(Template(r"tpl1598937156320.png", record_pos=(0.11, 0.107), resolution=(960, 540)))
                skip_pcr()
                touch(v=h_w_num(0.361,0.077),times=50)
                return
        if exists(Template(r"tpl1598938186547.png", threshold=0.9, rgb=True, record_pos=(0.152, 0.255), resolution=(960, 540))):
            touch(Template(r"tpl1598938186547.png", threshold=0.9, rgb=True, record_pos=(0.152, 0.255), resolution=(960, 540)))
            skip_pcr()
            sleep(5)
            touch(v=half_num,times=50)
            sleep(5)
            touch(v=half_num,times=50)
            sleep(5)
            touch(v=half_num,times=50)
            touch(Template(r"tpl1598938264248.png", record_pos=(0.44, 0.16), resolution=(960, 540)))
            touch(wait(Template(r"tpl1598938275967.png", record_pos=(0.0, 0.216), resolution=(960, 540))))
            sleep(2)
            true_touch(Template(r"tpl1598938275967.png", record_pos=(0.0, 0.216), resolution=(960, 540)))
            sleep(3)
            fight_pcr_in()
            return
        if exists(Template(r"tpl1598938565217.png", record_pos=(0.015, -0.046), resolution=(960, 540))):
            touch(v=half_num,times=50)
            touch(Template(r"tpl1598938582533.png", record_pos=(0.374, -0.075), resolution=(960, 540)))
            sleep(2)
            touch(v=half_num,times=50)
            sleep(2)
            touch(v=half_num,times=50)
            fight_pcr_in()
            return
        if exists(Template(r"tpl1598938998295.png", threshold=0.9, rgb=True, record_pos=(0.0, 0.248), resolution=(960, 540))):
            touch(Template(r"tpl1598938998295.png", threshold=0.9, rgb=True, record_pos=(0.0, 0.248), resolution=(960, 540)))
            touch(Template(r"tpl1598939021275.png", record_pos=(0.264, -0.142), resolution=(960, 540)))
            sleep(2)
            touch(Template(r"tpl1598939094070.png", record_pos=(-0.405, 0.252), resolution=(960, 540)))
            sleep(3)
            true_touch(Template(r"tpl1598939114670.png", record_pos=(-0.116, 0.103), resolution=(960, 540)))

            fight_pcr_in()
            return
        if exists(Template(r"tpl1598939727599.png", record_pos=(0.228, -0.19), resolution=(960, 540))):
            touch(Template(r"tpl1598939727599.png", record_pos=(0.228, -0.19), resolution=(960, 540)))
            touch(v=half_num,times=50)
            touch(Template(r"tpl1598939837444.png", record_pos=(0.107, -0.19), resolution=(960, 540)))

            touch(Template(r"tpl1598939803335.png", record_pos=(-0.405, 0.248), resolution=(960, 540)))       
            fight_pcr_in()
            return
        if exists(Template(r"tpl1598940028826.png", threshold=0.9, rgb=True, record_pos=(0.406, 0.245), resolution=(960, 540))):
            touch(Template(r"tpl1598940028826.png", threshold=0.9, rgb=True, record_pos=(0.406, 0.245), resolution=(960, 540)))
            sleep(4)
            touch(Template(r"tpl1598940062185.png", record_pos=(0.098, -0.087), resolution=(960, 540)))
            touch(v=half_num,times=50)
            sleep(2)
            touch(v=half_num,times=20)
            fight_pcr_in()
            return
        if exists(Template(r"tpl1598940568334.png", record_pos=(0.071, -0.062), resolution=(960, 540))):
            touch(Template(r"tpl1598940568334.png", record_pos=(0.071, -0.062), resolution=(960, 540)))
            sleep(3)
            touch(v=half_num,times=50)
            touch(Template(r"tpl1598940608922.png", record_pos=(-0.354, 0.072), resolution=(960, 540)))
            sleep(5)
            skip_pcr()
            touch(v=half_num,times=50)
            touch(Template(r"tpl1598940655920.png", record_pos=(-0.4, 0.258), resolution=(960, 540)))
            sleep(2)
            return
            if exists(Template(r"tpl1598940685484.png", record_pos=(0.0, 0.106), resolution=(960, 540))):
                touch(Template(r"tpl1598940685484.png", record_pos=(0.0, 0.106), resolution=(960, 540)))
                touch(Template(r"tpl1598940731906.png", record_pos=(0.0, 0.089), resolution=(960, 540)))
                skip_pcr()
                touch(v=half_num,times=50)
                true_touch(Template(r"tpl1598940781551.png", record_pos=(-0.001, 0.106), resolution=(960, 540)))
                sleep(2)
                true_touch(Template(r"tpl1598940781551.png", record_pos=(-0.001, 0.106), resolution=(960, 540)))
                touch(Template(r"tpl1598940655920.png", record_pos=(-0.4, 0.258), resolution=(960, 540)))               
                fight_pcr_in()
                return
        if exists(Template(r"tpl1598941005769.png", record_pos=(-0.405, 0.255), resolution=(960, 540))):
            touch(Template(r"tpl1598941005769.png", record_pos=(-0.405, 0.255), resolution=(960, 540)))
            sleep(5)
            touch(Template(r"tpl1598941036159.png", record_pos=(0.222, 0.173), resolution=(960, 540)))
            touch(v=half_num,times=20)
            fight_pcr_in()
            return

def fight_pcr_on():
    true_touch(Template(r"tpl1598937412254.png", record_pos=(-0.002, 0.104), resolution=(960, 540)))
    sleep(1)
    true_touch(Template(r"tpl1598937412254.png", record_pos=(-0.002, 0.104), resolution=(960, 540)))
#     kekeluo_face()

    touch(Template(r"tpl1598965140708.png", record_pos=(0.304, 0.039), resolution=(960, 540)))
    challenge_go()
    sleep(5)
    wait(Template(r"tpl1598939281593.png", record_pos=(-0.464, -0.255), resolution=(960, 540)),timeout=60)
    if not exists(Template(r"tpl1598943793045.png", rgb=True, record_pos=(0.445, 0.163), resolution=(960, 540))):  
        true_touch(Template(r"tpl1598935607677.png", rgb=True, record_pos=(0.446, 0.157), resolution=(960, 540)))
        true_touch(Template(r"tpl1598935612805.png", rgb=True, record_pos=(0.446, 0.235), resolution=(960, 540)))
    wait(Template(r"tpl1598962536948.png", record_pos=(-0.47, -0.256), resolution=(960, 540)),timeout=60)
    flag_end =False
    if exists(Template(r"tpl1598937739455.png", threshold=0.9, rgb=True, record_pos=(0.008, -0.041), resolution=(960, 540))):
        while True:
            touch(v=half_num,times=50,duration=0.01)
            flag_end =True
            if exists(Template(r"tpl1598944730372.png", record_pos=(0.383, -0.245), resolution=(960, 540))):
                break
    win_exists()
    if flag_end:
        sleep(8)
def win_exists():    
    sleep(10)
    true_touch(Template(r"tpl1598936644675.png", record_pos=(-0.001, 0.104), resolution=(960, 540)))
    wait(Template(r"tpl1598944730372.png", record_pos=(0.383, -0.245), resolution=(960, 540)),timeout=180)
    sleep(2)
    if not exists(Template(r"tpl1598935673156.png", record_pos=(0.361, 0.231), resolution=(960, 540))):       
        for num in range(4):
            true_touch(Template(r"tpl1598941222448.png", record_pos=(0.428, -0.237), resolution=(960, 540)))
            sleep(1)
    next_in()
def challenge_go():
    touch(Template(r"tpl1598935247088.png", record_pos=(0.372, 0.19), resolution=(960, 540)))
    true_touch(Template(r"tpl1598935259390.png", threshold=0.9, rgb=True, record_pos=(-0.004, 0.171), resolution=(960, 540)))
    touch(Template(r"tpl1598935581863.png", record_pos=(0.367, 0.19), resolution=(960, 540)))
def next_in():
    touch(wait(Template(r"tpl1598935673156.png", record_pos=(0.361, 0.231), resolution=(960, 540)),timeout=60))
    sleep(2)
    true_touch(Template(r"tpl1598935697756.png", record_pos=(0.36, 0.227), resolution=(960, 540)))
    sleep(5)
def un_city():
    touch(Template(r"tpl1598942402769.png", record_pos=(0.004, 0.254), resolution=(960, 540)))
    touch(Template(r"tpl1598942428282.png", record_pos=(0.261, -0.136), resolution=(960, 540)))
    touch(Template(r"tpl1598942438127.png", record_pos=(0.346, 0.046), resolution=(960, 540)))
    lock_in()
    challenge_go()
    win_exists()
    touch(Template(r"tpl1598943181667.png", record_pos=(0.364, 0.228), resolution=(960, 540)))
    touch(Template(r"tpl1598943174828.png", threshold=0.9, rgb=True, record_pos=(0.193, 0.228), resolution=(960, 540)))
    challenge_go()
    win_exists()    
    touch(wait(Template(r"tpl1598942856629.png", record_pos=(0.362, 0.231), resolution=(960, 540))))
    touch(Template(r"tpl1598942870868.png", record_pos=(0.105, -0.008), resolution=(960, 540)))
    lock_in()
    challenge_go()
    win_exists()
    touch(Template(r"tpl1598943181667.png", record_pos=(0.364, 0.228), resolution=(960, 540)))
    touch(Template(r"tpl1598943174828.png", threshold=0.9, rgb=True, record_pos=(0.193, 0.228), resolution=(960, 540)))
    challenge_go()
    win_exists()
    touch(wait(Template(r"tpl1598942856629.png", record_pos=(0.362, 0.231), resolution=(960, 540))))
    sleep(3)
    touch(Template(r"tpl1598943438333.png", record_pos=(-0.402, 0.254), resolution=(960, 540)))

def lock_in():
    if exists(Template(r"tpl1598942461624.png", record_pos=(0.014, -0.16), resolution=(960, 540))):
        touch(v=h_w_num(0.565,0.471),times=10,duration=0.1)        
    else:
        touch(v=h_w_num(0.576,0.26),times=10,duration=0.1)

def skip_pcr():
    touch(wait(Template(r"tpl1598937027067.png", record_pos=(0.451, -0.233), resolution=(960, 540))))
    touch(Template(r"tpl1598937043665.png", record_pos=(0.334, -0.232), resolution=(960, 540)))
    touch(Template(r"tpl1598937049607.png", record_pos=(0.111, 0.106), resolution=(960, 540)))




height_width()
# for num in range(10):
#     fight_pcr_on()

# while True:
#     touch(v=h_w_num(0.932,0.069),times=50,duration=0.1)
#     if touch(Template(r"tpl1598950919210.png", record_pos=(0.115, 0.181), resolution=(960, 540))):
#         touch(v=h_w_num(0.612,0.81),times=20,duration=0.1)

        
def next_movie_01():
    while True:
        if exists(Template(r"tpl1598951397441.png", record_pos=(0.022, -0.193), resolution=(960, 540))):
            touch(v=h_w_num(0.694,0.238),times=5,duration=0.1)
            while True:
                if exists(Template(r"tpl1598951702357.png", record_pos=(0.018, -0.075), resolution=(960, 540))):
                    if exists(Template(r"tpl1598951509067.png", record_pos=(0.017, -0.178), resolution=(960, 540))):
                        touch(v=h_w_num(0.643,0.455),times=5,duration=0.1)
                    else:
                        touch(v=h_w_num(0.694,0.238),times=5,duration=0.1)
                    touch(Template(r"tpl1598975051331.png", record_pos=(0.001, 0.085), resolution=(960, 540)))

                    touch(Template(r"tpl1598951589313.png", record_pos=(0.455, -0.24), resolution=(960, 540)))
                    touch(Template(r"tpl1598975077061.png", record_pos=(0.336, -0.233), resolution=(960, 540)))
                    touch(Template(r"tpl1598975090671.png", record_pos=(0.117, 0.103), resolution=(960, 540)))

                    touch(Template(r"tpl1598975103484.png", record_pos=(-0.003, 0.167), resolution=(960, 540)))
                else:
                    touch(Template(r"tpl1598951798469.png", record_pos=(-0.467, -0.246), resolution=(960, 540)))
                    break
        else:
            break
def skip_music():
    touch(wait(Template(r"tpl1598951589313.png", record_pos=(0.455, -0.24), resolution=(960, 540)),timeout=60))                
    touch(Template(r"tpl1598956381927.png", record_pos=(0.34, -0.233), resolution=(960, 540)))

    touch(Template(r"tpl1598956391738.png", record_pos=(0.111, 0.105), resolution=(960, 540)))

def next_movie_02():
    while  True:
        if exists(Template(r"tpl1598957403009.png", record_pos=(0.364, -0.182), resolution=(960, 540))):
            touch(v=h_w_num(0.636,0.267),times=3,duration=0.1)
            touch(Template(r"tpl1598957433591.png", record_pos=(-0.118, 0.175), resolution=(960, 540)))
            touch(Template(r"tpl1598957442154.png", record_pos=(0.113, 0.11), resolution=(960, 540)))
            touch(Template(r"tpl1598957452537.png", record_pos=(0.007, 0.219), resolution=(960, 540)))
            sleep(1)
            touch(Template(r"tpl1598957465026.png", record_pos=(-0.466, -0.248), resolution=(960, 540)),times=3)
        if exists(Template(r"tpl1598953105751.png", record_pos=(0.026, -0.196), resolution=(960, 540))):

            if exists(Template(r"tpl1598951509067.png", record_pos=(0.017, -0.178), resolution=(960, 540))):
                touch(v=h_w_num(0.612,0.45),times=3,duration=0.1)
            else:            
                touch(v=h_w_num(0.622,0.22),times=3,duration=0.1)
            while True:
                if exists(Template(r"tpl1598953068297.png", record_pos=(0.024, -0.074), resolution=(960, 540))):
                    if exists(Template(r"tpl1598951509067.png", record_pos=(0.017, -0.178), resolution=(960, 540))):
                        touch(v=h_w_num(0.643,0.455),times=3,duration=0.1)
                    else:
                        touch(v=h_w_num(0.694,0.238),times=3,duration=0.1)
                    touch(wait(Template(r"tpl1598957296455.png", threshold=0.8, rgb=True, target_pos=8, record_pos=(-0.001, 0.059), resolution=(960, 540))))

                    while True:
                        touch(v=half_num,times=10,duration=0.1)                 
                        true_touch(Template(r"tpl1598956445731.png", record_pos=(0.116, 0.105), resolution=(960, 540)))                     
                        
                        if exists(Template(r"tpl1598951589313.png", record_pos=(0.455, -0.24), resolution=(960, 540))):
                            skip_music()   
                        if  exists(Template(r"tpl1598956470707.png", record_pos=(-0.001, 0.168), resolution=(960, 540))):

                            break                                                   
                    touch(Template(r"tpl1598956470707.png", record_pos=(-0.001, 0.168), resolution=(960, 540)))
                else:
                    touch(Template(r"tpl1598951798469.png", record_pos=(-0.467, -0.246), resolution=(960, 540)))
                    break
        else:
            touch(Template(r"tpl1598952522436.png", record_pos=(-0.466, -0.248), resolution=(960, 540)))
            break



next_movie_01()


    
# un_city()
# id_pcr = import_id()[2]
# sign_in(id_pcr)
# login_system()
# set_one_movie()



    


